
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 12/27/2011 22:52:16
-- Generated from EDMX file: D:\РАБОТЫ\Новый Привозъ\OtgruzkaSolution\DataTier\DBModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [OTGRUZKA];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Account_Order]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Account] DROP CONSTRAINT [FK_Account_Order];
GO
IF OBJECT_ID(N'[dbo].[FK_Account_State]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Account] DROP CONSTRAINT [FK_Account_State];
GO
IF OBJECT_ID(N'[dbo].[FK_Account_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Account] DROP CONSTRAINT [FK_Account_User];
GO
IF OBJECT_ID(N'[dbo].[FK_Account_User1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Account] DROP CONSTRAINT [FK_Account_User1];
GO
IF OBJECT_ID(N'[dbo].[FK_CostsCenter_CostsCenter]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CostsCenter] DROP CONSTRAINT [FK_CostsCenter_CostsCenter];
GO
IF OBJECT_ID(N'[dbo].[FK_Department_JuridicalPerson]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Department] DROP CONSTRAINT [FK_Department_JuridicalPerson];
GO
IF OBJECT_ID(N'[dbo].[FK_JuridicalPerson_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[JuridicalPerson] DROP CONSTRAINT [FK_JuridicalPerson_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Order_State]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Order] DROP CONSTRAINT [FK_Order_State];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderHeader_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Order] DROP CONSTRAINT [FK_OrderHeader_User];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderHeader_User1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Order] DROP CONSTRAINT [FK_OrderHeader_User1];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderSpec_OrderHeader]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderSpec] DROP CONSTRAINT [FK_OrderSpec_OrderHeader];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderSpec_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderSpec] DROP CONSTRAINT [FK_OrderSpec_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_Permission_PermissionCategory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Permission] DROP CONSTRAINT [FK_Permission_PermissionCategory];
GO
IF OBJECT_ID(N'[dbo].[FK_Product_ProductGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Product] DROP CONSTRAINT [FK_Product_ProductGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductGroup_ProductGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProductGroup] DROP CONSTRAINT [FK_ProductGroup_ProductGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_RolePermissions_Permission]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RolePermissions] DROP CONSTRAINT [FK_RolePermissions_Permission];
GO
IF OBJECT_ID(N'[dbo].[FK_RolePermissions_Role]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RolePermissions] DROP CONSTRAINT [FK_RolePermissions_Role];
GO
IF OBJECT_ID(N'[dbo].[FK_User_CostsCenter]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[User] DROP CONSTRAINT [FK_User_CostsCenter];
GO
IF OBJECT_ID(N'[dbo].[FK_UserRoles_Role]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRoles] DROP CONSTRAINT [FK_UserRoles_Role];
GO
IF OBJECT_ID(N'[dbo].[FK_UserRoles_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRoles] DROP CONSTRAINT [FK_UserRoles_User];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Account]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Account];
GO
IF OBJECT_ID(N'[dbo].[Company]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Company];
GO
IF OBJECT_ID(N'[dbo].[CostsCenter]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CostsCenter];
GO
IF OBJECT_ID(N'[dbo].[Department]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Department];
GO
IF OBJECT_ID(N'[dbo].[DocNumbers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DocNumbers];
GO
IF OBJECT_ID(N'[dbo].[JuridicalPerson]', 'U') IS NOT NULL
    DROP TABLE [dbo].[JuridicalPerson];
GO
IF OBJECT_ID(N'[dbo].[Order]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Order];
GO
IF OBJECT_ID(N'[dbo].[OrderSpec]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderSpec];
GO
IF OBJECT_ID(N'[dbo].[Permission]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Permission];
GO
IF OBJECT_ID(N'[dbo].[PermissionCategory]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PermissionCategory];
GO
IF OBJECT_ID(N'[dbo].[Product]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Product];
GO
IF OBJECT_ID(N'[dbo].[ProductGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProductGroup];
GO
IF OBJECT_ID(N'[dbo].[PromptingMessage]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PromptingMessage];
GO
IF OBJECT_ID(N'[dbo].[Role]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Role];
GO
IF OBJECT_ID(N'[dbo].[RolePermissions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RolePermissions];
GO
IF OBJECT_ID(N'[dbo].[State]', 'U') IS NOT NULL
    DROP TABLE [dbo].[State];
GO
IF OBJECT_ID(N'[dbo].[User]', 'U') IS NOT NULL
    DROP TABLE [dbo].[User];
GO
IF OBJECT_ID(N'[dbo].[UserRoles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserRoles];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Account'
CREATE TABLE [dbo].[Account] (
    [Id] int  NOT NULL,
    [OrderId] int  NOT NULL,
    [Number] nvarchar(50)  NULL,
    [CreatedUserId] int  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [ModifiedUserId] int  NULL,
    [ModifiedDate] datetime  NULL,
    [StateId] int  NOT NULL
);
GO

-- Creating table 'Company'
CREATE TABLE [dbo].[Company] (
    [CompanyId] int  NOT NULL,
    [CompanyName] nvarchar(400)  NULL,
    [Address] nvarchar(max)  NULL
);
GO

-- Creating table 'CostsCenter'
CREATE TABLE [dbo].[CostsCenter] (
    [CostsCenterId] int  NOT NULL,
    [Name] nvarchar(400)  NULL,
    [DepartmentId] int  NULL
);
GO

-- Creating table 'Department'
CREATE TABLE [dbo].[Department] (
    [DepartmentId] int  NOT NULL,
    [Name] nvarchar(400)  NULL,
    [JuridicalPersonId] int  NULL,
    [OfficeNumber] nvarchar(max)  NULL
);
GO

-- Creating table 'DocNumbers'
CREATE TABLE [dbo].[DocNumbers] (
    [DocType] nvarchar(128)  NOT NULL,
    [NextNumber] int  NOT NULL
);
GO

-- Creating table 'JuridicalPerson'
CREATE TABLE [dbo].[JuridicalPerson] (
    [JuridicalPersonId] int  NOT NULL,
    [Name] nvarchar(400)  NULL,
    [CompanyId] int  NOT NULL
);
GO

-- Creating table 'Order'
CREATE TABLE [dbo].[Order] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Number] nvarchar(50)  NULL,
    [CreatedUserId] int  NOT NULL,
    [CreatedDate] datetime  NOT NULL,
    [ModifiedUserId] int  NULL,
    [ModifiedDate] datetime  NULL,
    [ItogoSumm] float  NULL,
    [StateId] int  NOT NULL,
    [Description] nvarchar(max)  NULL,
    [ContactName] nvarchar(400)  NULL,
    [ContactPhone] nvarchar(256)  NULL,
    [ContactEmail] nvarchar(256)  NULL
);
GO

-- Creating table 'OrderSpec'
CREATE TABLE [dbo].[OrderSpec] (
    [OrderId] int  NOT NULL,
    [RowId] int  NOT NULL,
    [ProductId] int  NULL,
    [Count] int  NULL,
    [Summa] float  NULL
);
GO

-- Creating table 'Product'
CREATE TABLE [dbo].[Product] (
    [ProductId] int IDENTITY(1,1) NOT NULL,
    [Artikul] nvarchar(50)  NULL,
    [ProductName] nvarchar(400)  NULL,
    [Price] float  NULL,
    [GroupId] int  NULL,
    [IsUnavailable] bit  NULL,
    [ImageBigUrl] nvarchar(400)  NULL,
    [ImageSmallUrl] nvarchar(400)  NULL
);
GO

-- Creating table 'ProductGroup'
CREATE TABLE [dbo].[ProductGroup] (
    [GroupId] int IDENTITY(1,1) NOT NULL,
    [ParentGroupId] int  NULL,
    [GroupName] nvarchar(255)  NULL
);
GO

-- Creating table 'PromptingMessage'
CREATE TABLE [dbo].[PromptingMessage] (
    [PromptId] int IDENTITY(1,1) NOT NULL,
    [Number] int  NOT NULL,
    [Message] nvarchar(max)  NULL,
    [ForPage] nvarchar(400)  NULL
);
GO

-- Creating table 'State'
CREATE TABLE [dbo].[State] (
    [Id] int  NOT NULL,
    [Name] nvarchar(255)  NULL,
    [Color] nvarchar(50)  NULL,
    [Description] nvarchar(400)  NULL
);
GO

-- Creating table 'User'
CREATE TABLE [dbo].[User] (
    [UserId] int IDENTITY(1,1) NOT NULL,
    [FIO] nvarchar(400)  NULL,
    [Login] nvarchar(50)  NULL,
    [PassHash] nvarchar(50)  NULL,
    [CostsCenterId] int  NOT NULL,
    [Email] nvarchar(50)  NULL,
    [Phone] nvarchar(50)  NULL
);
GO

-- Creating table 'UserRoles'
CREATE TABLE [dbo].[UserRoles] (
    [RoleId] int  NOT NULL,
    [UserId] int  NOT NULL
);
GO

-- Creating table 'Permission'
CREATE TABLE [dbo].[Permission] (
    [PermissionId] int IDENTITY(1,1) NOT NULL,
    [PCategoryId] int  NULL,
    [PermissionName] nvarchar(400)  NULL
);
GO

-- Creating table 'PermissionCategory'
CREATE TABLE [dbo].[PermissionCategory] (
    [CategoryId] int IDENTITY(1,1) NOT NULL,
    [CategoryName] nvarchar(255)  NULL
);
GO

-- Creating table 'Role'
CREATE TABLE [dbo].[Role] (
    [RoleId] int IDENTITY(1,1) NOT NULL,
    [RoleName] nvarchar(400)  NULL
);
GO

-- Creating table 'RolePermissions'
CREATE TABLE [dbo].[RolePermissions] (
    [Permission_PermissionId] int  NOT NULL,
    [Role_RoleId] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [PK_Account]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [CompanyId] in table 'Company'
ALTER TABLE [dbo].[Company]
ADD CONSTRAINT [PK_Company]
    PRIMARY KEY CLUSTERED ([CompanyId] ASC);
GO

-- Creating primary key on [CostsCenterId] in table 'CostsCenter'
ALTER TABLE [dbo].[CostsCenter]
ADD CONSTRAINT [PK_CostsCenter]
    PRIMARY KEY CLUSTERED ([CostsCenterId] ASC);
GO

-- Creating primary key on [DepartmentId] in table 'Department'
ALTER TABLE [dbo].[Department]
ADD CONSTRAINT [PK_Department]
    PRIMARY KEY CLUSTERED ([DepartmentId] ASC);
GO

-- Creating primary key on [DocType] in table 'DocNumbers'
ALTER TABLE [dbo].[DocNumbers]
ADD CONSTRAINT [PK_DocNumbers]
    PRIMARY KEY CLUSTERED ([DocType] ASC);
GO

-- Creating primary key on [JuridicalPersonId] in table 'JuridicalPerson'
ALTER TABLE [dbo].[JuridicalPerson]
ADD CONSTRAINT [PK_JuridicalPerson]
    PRIMARY KEY CLUSTERED ([JuridicalPersonId] ASC);
GO

-- Creating primary key on [Id] in table 'Order'
ALTER TABLE [dbo].[Order]
ADD CONSTRAINT [PK_Order]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [OrderId], [RowId] in table 'OrderSpec'
ALTER TABLE [dbo].[OrderSpec]
ADD CONSTRAINT [PK_OrderSpec]
    PRIMARY KEY CLUSTERED ([OrderId], [RowId] ASC);
GO

-- Creating primary key on [ProductId] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [PK_Product]
    PRIMARY KEY CLUSTERED ([ProductId] ASC);
GO

-- Creating primary key on [GroupId] in table 'ProductGroup'
ALTER TABLE [dbo].[ProductGroup]
ADD CONSTRAINT [PK_ProductGroup]
    PRIMARY KEY CLUSTERED ([GroupId] ASC);
GO

-- Creating primary key on [PromptId] in table 'PromptingMessage'
ALTER TABLE [dbo].[PromptingMessage]
ADD CONSTRAINT [PK_PromptingMessage]
    PRIMARY KEY CLUSTERED ([PromptId] ASC);
GO

-- Creating primary key on [Id] in table 'State'
ALTER TABLE [dbo].[State]
ADD CONSTRAINT [PK_State]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [UserId] in table 'User'
ALTER TABLE [dbo].[User]
ADD CONSTRAINT [PK_User]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [RoleId], [UserId] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [PK_UserRoles]
    PRIMARY KEY NONCLUSTERED ([RoleId], [UserId] ASC);
GO

-- Creating primary key on [PermissionId] in table 'Permission'
ALTER TABLE [dbo].[Permission]
ADD CONSTRAINT [PK_Permission]
    PRIMARY KEY CLUSTERED ([PermissionId] ASC);
GO

-- Creating primary key on [CategoryId] in table 'PermissionCategory'
ALTER TABLE [dbo].[PermissionCategory]
ADD CONSTRAINT [PK_PermissionCategory]
    PRIMARY KEY CLUSTERED ([CategoryId] ASC);
GO

-- Creating primary key on [RoleId] in table 'Role'
ALTER TABLE [dbo].[Role]
ADD CONSTRAINT [PK_Role]
    PRIMARY KEY CLUSTERED ([RoleId] ASC);
GO

-- Creating primary key on [Permission_PermissionId], [Role_RoleId] in table 'RolePermissions'
ALTER TABLE [dbo].[RolePermissions]
ADD CONSTRAINT [PK_RolePermissions]
    PRIMARY KEY NONCLUSTERED ([Permission_PermissionId], [Role_RoleId] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [OrderId] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [FK_Account_Order]
    FOREIGN KEY ([OrderId])
    REFERENCES [dbo].[Order]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Account_Order'
CREATE INDEX [IX_FK_Account_Order]
ON [dbo].[Account]
    ([OrderId]);
GO

-- Creating foreign key on [StateId] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [FK_Account_State]
    FOREIGN KEY ([StateId])
    REFERENCES [dbo].[State]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Account_State'
CREATE INDEX [IX_FK_Account_State]
ON [dbo].[Account]
    ([StateId]);
GO

-- Creating foreign key on [CreatedUserId] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [FK_Account_User]
    FOREIGN KEY ([CreatedUserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Account_User'
CREATE INDEX [IX_FK_Account_User]
ON [dbo].[Account]
    ([CreatedUserId]);
GO

-- Creating foreign key on [ModifiedUserId] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [FK_Account_User1]
    FOREIGN KEY ([ModifiedUserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Account_User1'
CREATE INDEX [IX_FK_Account_User1]
ON [dbo].[Account]
    ([ModifiedUserId]);
GO

-- Creating foreign key on [CompanyId] in table 'JuridicalPerson'
ALTER TABLE [dbo].[JuridicalPerson]
ADD CONSTRAINT [FK_JuridicalPerson_Company]
    FOREIGN KEY ([CompanyId])
    REFERENCES [dbo].[Company]
        ([CompanyId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_JuridicalPerson_Company'
CREATE INDEX [IX_FK_JuridicalPerson_Company]
ON [dbo].[JuridicalPerson]
    ([CompanyId]);
GO

-- Creating foreign key on [DepartmentId] in table 'CostsCenter'
ALTER TABLE [dbo].[CostsCenter]
ADD CONSTRAINT [FK_CostsCenter_CostsCenter]
    FOREIGN KEY ([DepartmentId])
    REFERENCES [dbo].[Department]
        ([DepartmentId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CostsCenter_CostsCenter'
CREATE INDEX [IX_FK_CostsCenter_CostsCenter]
ON [dbo].[CostsCenter]
    ([DepartmentId]);
GO

-- Creating foreign key on [CostsCenterId] in table 'User'
ALTER TABLE [dbo].[User]
ADD CONSTRAINT [FK_User_CostsCenter]
    FOREIGN KEY ([CostsCenterId])
    REFERENCES [dbo].[CostsCenter]
        ([CostsCenterId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_User_CostsCenter'
CREATE INDEX [IX_FK_User_CostsCenter]
ON [dbo].[User]
    ([CostsCenterId]);
GO

-- Creating foreign key on [JuridicalPersonId] in table 'Department'
ALTER TABLE [dbo].[Department]
ADD CONSTRAINT [FK_Department_JuridicalPerson]
    FOREIGN KEY ([JuridicalPersonId])
    REFERENCES [dbo].[JuridicalPerson]
        ([JuridicalPersonId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Department_JuridicalPerson'
CREATE INDEX [IX_FK_Department_JuridicalPerson]
ON [dbo].[Department]
    ([JuridicalPersonId]);
GO

-- Creating foreign key on [StateId] in table 'Order'
ALTER TABLE [dbo].[Order]
ADD CONSTRAINT [FK_Order_State]
    FOREIGN KEY ([StateId])
    REFERENCES [dbo].[State]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Order_State'
CREATE INDEX [IX_FK_Order_State]
ON [dbo].[Order]
    ([StateId]);
GO

-- Creating foreign key on [CreatedUserId] in table 'Order'
ALTER TABLE [dbo].[Order]
ADD CONSTRAINT [FK_OrderHeader_User]
    FOREIGN KEY ([CreatedUserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderHeader_User'
CREATE INDEX [IX_FK_OrderHeader_User]
ON [dbo].[Order]
    ([CreatedUserId]);
GO

-- Creating foreign key on [ModifiedUserId] in table 'Order'
ALTER TABLE [dbo].[Order]
ADD CONSTRAINT [FK_OrderHeader_User1]
    FOREIGN KEY ([ModifiedUserId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderHeader_User1'
CREATE INDEX [IX_FK_OrderHeader_User1]
ON [dbo].[Order]
    ([ModifiedUserId]);
GO

-- Creating foreign key on [OrderId] in table 'OrderSpec'
ALTER TABLE [dbo].[OrderSpec]
ADD CONSTRAINT [FK_OrderSpec_OrderHeader]
    FOREIGN KEY ([OrderId])
    REFERENCES [dbo].[Order]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [ProductId] in table 'OrderSpec'
ALTER TABLE [dbo].[OrderSpec]
ADD CONSTRAINT [FK_OrderSpec_Product]
    FOREIGN KEY ([ProductId])
    REFERENCES [dbo].[Product]
        ([ProductId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderSpec_Product'
CREATE INDEX [IX_FK_OrderSpec_Product]
ON [dbo].[OrderSpec]
    ([ProductId]);
GO

-- Creating foreign key on [GroupId] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [FK_Product_ProductGroup]
    FOREIGN KEY ([GroupId])
    REFERENCES [dbo].[ProductGroup]
        ([GroupId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Product_ProductGroup'
CREATE INDEX [IX_FK_Product_ProductGroup]
ON [dbo].[Product]
    ([GroupId]);
GO

-- Creating foreign key on [ParentGroupId] in table 'ProductGroup'
ALTER TABLE [dbo].[ProductGroup]
ADD CONSTRAINT [FK_ProductGroup_ProductGroup]
    FOREIGN KEY ([ParentGroupId])
    REFERENCES [dbo].[ProductGroup]
        ([GroupId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductGroup_ProductGroup'
CREATE INDEX [IX_FK_ProductGroup_ProductGroup]
ON [dbo].[ProductGroup]
    ([ParentGroupId]);
GO

-- Creating foreign key on [RoleId] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [FK_UserRoles_User]
    FOREIGN KEY ([RoleId])
    REFERENCES [dbo].[User]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [PCategoryId] in table 'Permission'
ALTER TABLE [dbo].[Permission]
ADD CONSTRAINT [FK_Permission_PermissionCategory]
    FOREIGN KEY ([PCategoryId])
    REFERENCES [dbo].[PermissionCategory]
        ([CategoryId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Permission_PermissionCategory'
CREATE INDEX [IX_FK_Permission_PermissionCategory]
ON [dbo].[Permission]
    ([PCategoryId]);
GO

-- Creating foreign key on [RoleId] in table 'UserRoles'
ALTER TABLE [dbo].[UserRoles]
ADD CONSTRAINT [FK_UserRoles_Role]
    FOREIGN KEY ([RoleId])
    REFERENCES [dbo].[Role]
        ([RoleId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Permission_PermissionId] in table 'RolePermissions'
ALTER TABLE [dbo].[RolePermissions]
ADD CONSTRAINT [FK_RolePermissions_Permission]
    FOREIGN KEY ([Permission_PermissionId])
    REFERENCES [dbo].[Permission]
        ([PermissionId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Role_RoleId] in table 'RolePermissions'
ALTER TABLE [dbo].[RolePermissions]
ADD CONSTRAINT [FK_RolePermissions_Role]
    FOREIGN KEY ([Role_RoleId])
    REFERENCES [dbo].[Role]
        ([RoleId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RolePermissions_Role'
CREATE INDEX [IX_FK_RolePermissions_Role]
ON [dbo].[RolePermissions]
    ([Role_RoleId]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------